<?php

namespace Quiz\Controller;
use Think\Controller;

class QuizController extends Controller {


    /**
     * 给出预警用户的列表
     * @param $userlist
     * @param int $oid
     * @param string $group_quiztitle
     * @return mixed
     */

    public function WarnUserList($userorgdata, $oid = 0,$quiz_name , $group_quiztitle = "")
    {

        if(!$oid && !$group_quiztitle){
            $warnvalue = M('quiz_info')->field('warnvalue')->where(['quiz_id' => 6])->select();
        }
        else{
            $warnvalue = M('orgnizer_grouptest')->field('warnvalue')->where(['title' => $group_quiztitle, 'oid' => $oid])->select();
        }

        $warnvalue = $warnvalue[0]['warnvalue'];
        $warnvalue = json_decode($warnvalue, true);

        $warnvalue = $warnvalue[$quiz_name];

        $section = [];
        //获取键值
        for ($i = 0; $i < count($warnvalue); $i++){
            $key = array_keys(array_slice($warnvalue[$i], 0, 1));
            $key = $key[0];
            $section[$i] = $key;
        }
        $i = 0;

        foreach($userorgdata as &$u){//$u位每一个用户


            if (!$warnvalue){//没有设定预警值的情况
                foreach($u['score_data'] as &$score){
                    $score['status'] = 0;
                }
                $u['warn'] = 0;
            }
            else{
                foreach($warnvalue as $w){
                    $choosen = $section[$i];
                    if ((int)$w[$choosen]){

                        //判断使用原始分还是转换分（转换分是0或-1使用原始分）



                        switch ($w['select']){//操作符设定
                            case 0:{//小于
                                if ((int)$u['score_data'][$i]['trans_score'] < (int)$w[$choosen]){
                                    $u['score_data'][$i]['status'] = 1;
                                    $u['warn'] = 1;
                                }
                                else{
                                    $u['score_data'][$i]['status'] = 0;//0正常
                                }
                                break;
                            }

                            case 1:{//大于
                                if ((int)$u['score_data'][$i]['trans_score'] > (int)$w[$choosen]){
                                    $u['score_data'][$i]['status'] = 1;
                                    $u['warn'] = 1;
                                }
                                else{
                                    $u['score_data'][$i]['status'] = 0;//0正常
                                }
                                break;
                            }

                        }
                        $i ++;
                    }
                    else{
                        //不作预警判断
                        //$u['warn'] = 0;
                        $u['score_data'][$i]['status'] = 0;//0正常
                        $i ++;
                    }
                }

            }

            $i = 0;

        }

        //返回标识后的数据
        return $userorgdata;
    }


    public function getWarnValue($oid = 0, $quiz_grouptitle=""){

        //返回一个数组带测试问卷的名称

//        if ($oid ==0 && $quiz_grouptitle = ""){
//
//            foreach(M("quiz_info")->select() as $row){
//                $quiz_list[] = $row['quiz_name'];
//            }
//
//            //查询json字串预警值设定
//            $warn_value = M("quiz_info")->field('warnvalue')->where(['oid' => $oid, 'title' => $quiz_grouptitle])->select();
//
//        }


        $orgnizer_grouptest_dao =  M("orgnizer_grouptest");

        $quiz_list_json =$orgnizer_grouptest_dao->where(['oid' => $oid, 'quiz_title' => $quiz_grouptitle])->select();
        $quiz_list_json = $quiz_list_json[0]['quiz_list'];
        //查询json字串预警值设定
        $warn_value = $orgnizer_grouptest_dao->field('warnvalue')->where(['oid' => $oid, 'title' => $quiz_grouptitle])->select();

        $warn_value = json_decode($warn_value[0]['warnvalue'], true);

        $quiz_list = json_decode($quiz_list_json, true);

        $elements['quiz_list'] = $quiz_list;//带quiz_name的数组

        foreach($quiz_list as $quiz_name){

            $value_list = M("element_info")
                ->join("quiz_info on quiz_info.quiz_id = element_info.quiz_id")
                ->field('element_name, element_label')
                ->where(["quiz_info.quiz_name" => $quiz_name])->select();

            $warnvalue = $warn_value[$quiz_name];

            if (!$warnvalue){//如果没有设定过预警值，设定列表初始值
                for ($i =0 ;$i < count($value_list); $i++){
                    $value_list[$i]['warn_value'] = 0;
                    $value_list[$i]['warn_select'] = 1;
                }
            }
            else{
                $i = 0;
                foreach($warnvalue as $row){
                    $value_list[$i]['warn_value'] = $row[$value_list[$i]['element_label']];
                    $value_list[$i++]['warn_select'] = $row['select'];
                }
            }

            $elements['warnvalue_list'][$quiz_name] = $value_list;
        }

        //var_dump($elements['warnvalue_list']);

        $this->ajaxReturn($elements);
    }


//    public function getWarnValue($oid = 0 ,$quiztitle = null, $quiz_name)
//    {
//        if(IS_AJAX){
//
//            if (!$oid  && !$quiztitle){//参数为空即为普通用户预警设置
//                $rs = M('quiz_info')->field('warnvalue')->where(['quiz_id' => 1])->select();
//            }
//            else{
//                $rs = M('orgnizer_grouptest')->field('warnvalue')->where(['oid' => $oid, 'title' => $quiztitle])->select();
//            }
//
//            $rs = json_decode($rs[0]['warnvalue'], true);
//            $rs = $rs[$quiz_name];
//
//            $elements = M("element_info")
//                ->join("quiz_info on quiz_info.quiz_id = element_info.quiz_id")
//                ->where(["quiz_info.quiz_name" => $quiz_name])->select();
//
//            $section = [];
//
//            $i = 0;
//            foreach($elements as $e){
//                $section[$i] = $e['element_label'];
//                $i ++;
//            }
//
//            if (!$rs){
//                for ($i =0 ;$i < count($elements); $i++){
//                    $rs[$i][$section[$i]] = 0;
//                    $rs[$i]['select'] = 1;
//                }
//            }
//
//            $rs['element_labels'] =$section;
//
//            $this->ajaxReturn($rs);
//        }
//
//    }

    public function setWarnValue($oid = 0, $quiz_grouptitle = null)
    {
        $jsondata = I_withoutReplace('jsondata');

        if (!$oid  && !$quiz_grouptitle){
            M('quiz_info')->where(['quiz_id' => 6])->save(['warnvalue' => $jsondata]);
        }//参数为空即为普通用户预警设置
        else{
            M('orgnizer_grouptest')->where(['oid' => $oid, 'title' => $quiz_grouptitle])->save(['warnvalue' => $jsondata]);
        }



        $rs['msg'] = '操作成功！';
        $this->ajaxReturn($rs);
    }


    /**
     * 获取测试的问卷quiz_name和每个问卷的属性名称
     * @param int $oid
     * @param $quiz_grouptitle
     */



}